package com.hh.demo.web.controller;

import com.hh.demo.constants.ExceptionEnum;
import com.hh.demo.dto.TelCodeDto;
import com.hh.demo.exception.GlobalException;
import com.hh.demo.result.JSONResult;
import com.hh.demo.service.IVerifyCodeService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author 黄浩
 * @create 2022-06-15 16:34
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Resource
    private IVerifyCodeService verifyCodeService;

    /**
     * 图片验证码
     * @return
     */
    @GetMapping("/image/{imageCodeKey}")
    public JSONResult image(@PathVariable String imageCodeKey){
        if (StringUtils.isEmpty(imageCodeKey))
            throw new GlobalException(ExceptionEnum.NULL_EXCEPTION.getMessage(),ExceptionEnum.NULL_EXCEPTION.getCode());
        String base64Str = verifyCodeService.image(imageCodeKey);
        return JSONResult.success(base64Str);
    }

    /**
     * 发送手机验证码
     * @return
     */
    @PostMapping("/sendTelCode")
    public JSONResult sendTelCode(@RequestBody @Valid TelCodeDto telCodeDto){
        verifyCodeService.sendTelCode(telCodeDto);
        return JSONResult.success();
    }
}
